<template>
  <div class="specs-page" v-loading="loading" element-loading-text="加载中...">
    <div class="specs-title">
      <div class="specs-title-left">
        <el-form inline>
          <el-form-item>
            <el-input
              v-model="searchForm.name"
              placeholder="规格名称"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-select v-model="searchForm.status" clearable placeholder="状态">
              <el-option label="启用" :value="1" />
              <el-option label="禁用" :value="0" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="searchFun">查询</el-button>
            <el-button @click="resetFun">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="specs-title-right">
        <el-button type="primary" :icon="Plus" @click="addDeptFun('add')">
          添加
        </el-button>
      </div>
    </div>
    <div class="specs-table">
      <el-table
        @selection-change="handleSelectionChange"
        border
        stripe
        :data="tableData"
        :height="store.tableHeight - 40"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="name" label="规格名称" width="200" />
        <el-table-column label="规格类型" width="150">
          <template #default="{ row }">
            <el-tag v-if="row.type == '系统预设'">系统预设</el-tag>
            <el-tag v-else-if="row.type == '用户自建'" type="warning">
              用户自建
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column prop="status" label="状态" width="90">
          <template #default="{ row }">
            <el-switch
              v-model="row.status"
              active-text="启用"
              :active-value="1"
              inactive-text="禁用"
              :inactive-value="0"
              inline-prompt
              @change="statusFun(row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="规格属性">
          <template #default="{ row }">
            {{ row.spec }}
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column prop="created_user" label="创建人" />

        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button
              type="primary"
              link
              @click="updateDeptFun('update', row)"
              v-if="row.id !== 1"
            >
              编辑
            </el-button>

            <el-button
              type="danger"
              v-if="row.type == '用户自建'"
              link
              @click="delFun(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="specs-table-foot" style="margin-top: 10px">
        <div>
          <el-button
            type="danger"
            :disabled="selectData.length == 0 ? true : false"
            @click="delAllFun"
          >
            <template #icon>
              <IEpDelete />
            </template>
            批量删除</el-button
          >
        </div>
        <el-pagination
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[20, 50, 100, 200]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <div></div>
      </div>
    </div>
  </div>
  <addDialog ref="addDialogRef" @refreshList="refreshList" />
</template>
<script setup>
import { Plus } from "@element-plus/icons-vue";
import { useCounterStore } from "@/stores/counter.js";
import { listApi, delApi, stateApi } from "@/plugins/api/shopping/specs";
import addDialog from "./components/DialogSpecs.vue";
const store = useCounterStore();
const router = useRouter();
let tableData = ref([]);

let loading = ref(false);
let searchForm = ref({});
function searchFun() {
  currentPage.value = 1;
  selectData.value = [];
  tableData.value = [];
  getData();
}
function resetFun() {
  searchForm.value = {};
  tableData.value = [];
  selectData.value = [];
  getData();
}
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  selectData.value = [];
  getData();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  selectData.value = [];
  getData();
};
function getData() {
  loading.value = true;
  let obj = Object.assign(
    {
      page: currentPage.value,
      pageSize: pageSize.value,
    },
    searchForm.value
  );
  listApi(obj)
    .then((data) => {
      tableData.value = data.list;
      total.value = data.page.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
let selectData = ref([]);
function handleSelectionChange(val) {
  selectData.value = val;
}
function delAllFun() {
  if (
    selectData.value.some((item) => {
      return item.type == "系统预设";
    })
  ) {
    return ElMessage.warning("请选择规格类型为'用户自建'的数据进行删除！");
  } else {
    ElMessageBox.confirm("确认批量删除选中的数据，是否继续?", "提示", {
      confirmButtonText: "确认",
      cancelButtonText: "取消",
      type: "warning",
    })
      .then(() => {
        let arr = selectData.value.map((ele) => {
          return ele.id;
        });
        delApi({
          id: arr.join(","),
        }).then(() => {
          ElMessage({
            type: "success",
            message: "删除成功！",
          });
          selectData.value = [];
          getData();
        });
      })
      .catch(() => {
        ElMessage({
          type: "info",
          message: "已取消。",
        });
      });
  }
}

let addDialogRef = ref(null);
function addDeptFun(type) {
  addDialogRef.value.initFun(type);
}
function updateDeptFun(type, row) {
  addDialogRef.value.initFun(type, row);
}
function refreshList() {
  getData();
}
function statusFun(row) {
  stateApi({
    status: row.status,
    id: row.id.toString(),
  }).then(() => {
    ElMessage({
      type: "success",
      message: "修改成功！",
    });
    getData();
  });
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
// 设置
function setDisFun(row) {
  router.push({
    path: "/system/dictItem",
    query: {
      dictId: row.id,
    },
  });
}
onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss">
.specs-page {
  width: 100%;
  height: 100%;

  .specs-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .specs-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      :deep(.el-form-item) {
        margin-bottom: 0 !important;
      }
    }
  }

  .specs-table {
    padding: 10px;
    box-sizing: border-box;

    .specs-table-foot {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
